var messageType = "";
var messageId = "";
$(function(){ 
	init();
});
function inputValue(value){
	messageType = value;
	$("span[name='selectMessageType']").each(function(){
		if($(this).attr('value') == value){
			$(this).attr('class','label label-success');
		}else{
			$(this).attr('class','label label-default');
		}
	})
	console.log(messageType);
}
function submit(){

	var editor = $(".w-e-text").html();
 	var title = $("#title").val();
 	var author = $("#author").val();
 	console.log(title);
 	if(title === "" || title === null){
   		layer.msg("请输入标题名称！", {icon: 5});
 		return false;
 	}else if(author === "" || author === null){
   		layer.msg("请输入作者名称！", {icon: 5});
 		return false;
 	}else if(editor === "" || editor === null){
   		layer.msg("请输入文章内容！", {icon: 5});
 		return false;
 	}else if(messageType == ""){
   		layer.msg("请选择文章类型", {icon: 5});
   		return false;
 	}
 	if(messageId != ""){
 		$.ajax({
	 	   	 type : "post",
	 	        url : "/TeaCulture/updatemessage/do",
	 	        data : { "author" : author , "title" : title , "content" : editor , "newMessage_id" : messageId},
	 	        dataType : "json",
	 	        traditional : true,
	 	        success : function(result){
	 	       	 if(result.result.code === "1"){
	 	       		 layer.msg("编辑新闻成功！", {icon: 6});
	 	       		 window.location.href="/TeaCulture/newmessagelist";
	 	       	 }else{
	  	       		layer.msg(result.result.des, {icon: 5});
	 	       	 }
	 	       	
	 	       },    
	 	       error : function(result){
	 	       		layer.msg(result.result.des, {icon: 5});
	 	       }
	 	});
 	}else{
	 		$.ajax({
	 	 	   	 type : "post",
	 	 	        url : "/TeaCulture/createNewMessage/do",
	 	 	        data : { "author" : author , "title" : title , "content" : editor, "messageType" : messageType},
	 	 	        dataType : "json",
	 	 	        traditional : true,
	 	 	        success : function(result){
	 	 	       	 if(result.result.code === "1"){
	 	 	       		 layer.msg("添加文章成功！", {icon: 6});
	 	 	       		 window.location.href="/TeaCulture/newmessagelist";
	 	 	       	 }else{
	 	 	       		layer.msg(result.result.des, {icon: 5});
	 	 	       	 }
	 	 	       	
	 	 	       },    
	 	 	       error : function(result){
	 	 	       		layer.msg(result.result.des, {icon: 5});
	 	 	       }
	 	 	});
 	}
 	
}
function init(){
	 $("#addBtn").on('click',function(){
		 	submit();
	 });
	 
}

